package com.msteg.b4a.app.wizard;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.witness.informacam.InformaCam;
import org.witness.informacam.models.organizations.IInstalledOrganizations;
import org.witness.informacam.models.organizations.IOrganization;
import org.witness.informacam.utils.Constants.App;
import org.witness.informacam.utils.Constants.IManifest;
import org.witness.informacam.utils.Constants.App.Storage.Type;
import org.witness.informacam.utils.Constants.WizardListener;

import com.msteg.b4a.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class InitOrganizationFragment extends Fragment implements WizardListener {
	private InformaCam informaCam;
	
	View rootView;
	Activity a;
	
	private final static String LOG = App.LOG;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater li, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(li, container, savedInstanceState);
		
		rootView = li.inflate(R.layout.fragment_wizard, null);
		
		return rootView;
	}
	
	@Override
	public void onAttach(Activity a) {
		super.onAttach(a);
		this.a = a;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private void initOrganization() {
		try {
			if(a.getAssets().list("includedOrganizations").length > 0) {
				List<IOrganization> includedOrganizations = new ArrayList<IOrganization>();
				for(String organizationManifest : a.getAssets().list("includedOrganizations")) {
					IOrganization organization = new IOrganization();
					organization.inflate((JSONObject) new JSONTokener(
							new String(
									informaCam.ioService.getBytes(
											("includedOrganizations/" + organizationManifest), 
											Type.APPLICATION_ASSET)
									)
							).nextValue());
					
					InputStream is = a.getResources().openRawResource(R.raw.bytes4all);
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					int size = 0;
					byte[] buf = new byte[1024];
					while((size = is.read(buf, 0, buf.length)) >= 0) {
						baos.write(buf, 0, size);
					}
					is.close();
					
					info.guardianproject.iocipher.File publicKey = new info.guardianproject.iocipher.File("bytes4all.asc");
					informaCam.ioService.saveBlob(baos.toByteArray(), publicKey);
					
					organization.publicKeyPath = publicKey.getAbsolutePath();
					includedOrganizations.add(organization);
				}

				IInstalledOrganizations installedOrganizations = new IInstalledOrganizations();
				installedOrganizations.organizations = includedOrganizations;
				informaCam.saveState(installedOrganizations, new info.guardianproject.iocipher.File(IManifest.ORGS));
			}
		} catch(IOException e) {
			Log.e(LOG, e.toString());
			e.printStackTrace();
		} catch (JSONException e) {
			Log.e(LOG, e.toString());
			e.printStackTrace();
		}
	}

	@Override
	public void onSubFragmentCompleted() {
		
		
	}

	@Override
	public FragmentManager returnFragmentManager() {
		return null;
	}

	@Override
	public void wizardCompleted() {}

	@Override
	public void onSubFragmentInitialized() {
		Log.d(LOG, "OK WIZARD HAS INITED ME");
		initOrganization();
	}
}
